package service;

import java.sql.ResultSet;
import java.sql.SQLException;


import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;

import base.BaseService;
import pojo.manager_infor;
public class ManagerService extends BaseService {
	private class UserRowMapper implements RowMapper<manager_infor> {

		public manager_infor mapRow(ResultSet rs, int rownum) throws SQLException {

			manager_infor inf = new manager_infor();
			inf.setId(rs.getInt("id"));
			inf.setUsername(rs.getString("username"));
			inf.setPassword(rs.getString("password"));
		
			return inf;
					}
			}
	
	//修改密码
	public int updatePassword(String password,int id){
		
		String sql="update managerInfor set password=? where id=?";
		
		return jdbcTemplate.update(sql,password,id);
	}
	//登录验证
	public manager_infor managerLogin(String username,String password){
		
		String sql = "select id,username,password from managerInfor where username=? and password=?";
		
		manager_infor inf = null;
		
		try{
			inf = jdbcTemplate.queryForObject(sql, new UserRowMapper(),username,password);
		
		}catch(EmptyResultDataAccessException e){
			
			}
		
		return inf;

		
	}
	public manager_infor getManagerByid(int id){
		class RowMapper implements org.springframework.jdbc.core.RowMapper<manager_infor> {

			public manager_infor mapRow(ResultSet rs, int rownum) throws SQLException {

				manager_infor inf = new manager_infor();
				inf.setId(rs.getInt("id"));
				inf.setUsername(rs.getString("username"));
				inf.setPassword(rs.getString("password"));
				inf.setReal_name(rs.getString("real_name"));
				inf.setIDcard_number(rs.getString("IDcard_number"));
				inf.setTel(rs.getString("tel"));
				inf.setPermit(rs.getInt("permit"));
			
				return inf;
						}
				}
		
		String sql="select * from managerInfor where id=?";
		manager_infor manager=this.jdbcTemplate.queryForObject(sql, new RowMapper(),id);
		return manager;
	}
	//修改个人信息
	public void getUpdateByid(String username,String real_name,String IDcard_number,String tel,int id){
		
		String sql ="update managerInfor set username=?,real_name=?, IDcard_number=?, tel=? where id=?";
		
		 jdbcTemplate.update(sql,username,real_name,IDcard_number,tel,id);
		}	
}
